package com.suse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suse.dao.EmployerDao;
import com.suse.dto.EmployerDto;
import com.suse.pojo.Employer;
import com.suse.service.EmployerService;
import com.suse.util.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Service
public class EmployerServiceImpl implements EmployerService {
    @Resource
    EmployerDao dao;
    @Override
    public ResultVo add(Employer employer) {
        try {
            int num = dao.insert(employer);
            if(num > 0) {
                return ResultVo.success("新增员工成功");
            }else {
                return ResultVo.error("新增员工失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error("数据异常");
        }
    }

    @Override
    public ResultVo update(Employer employer) {
        try {
            int num = dao.updateById(employer);
            if(num > 0) {
                return ResultVo.success("修改员工成功");
            }else {
                return ResultVo.error("修改员工失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error("数据异常");
        }
    }

    @Override
    public ResultVo del(Integer id) {
        try {
            int num = dao.deleteById(id);
            if(num > 0) {
                return ResultVo.success("删除员工成功");
            }else {
                return ResultVo.error("删除员工失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error("数据异常");
        }
    }

    @Override
    public ResultVo select(EmployerDto dto) {
        try {
            // 条件构造器
            QueryWrapper<Employer> queryWrapper = new QueryWrapper<>();
            // 用户名模糊查询
            boolean is = null != dto.getEmployerName() && !"".equals(dto.getEmployerName());
            queryWrapper.like(is, "employer_name",dto.getEmployerName());

            // 性别精确查询
            boolean iss = null != dto.getGender() && !"".equals(dto.getGender());
            queryWrapper.eq(iss, "gender",dto.getGender());

            //创建一个分页对象
            Page<Employer> page = new Page<>(dto.getPage(),dto.getRow());
            // 查询所有
            Page<Employer> infoPage = dao.selectPage(page,queryWrapper);
            if(infoPage.getRecords().size() > 0) {
                // 构造一个Map对象
                HashMap<String,Object> map = new HashMap<>();
                // 当前页的集合
                map.put("list",infoPage.getRecords());
                // 总条数
                map.put("total",infoPage.getTotal());

                return ResultVo.success("查询成功",map);
            }else {
                return ResultVo.error("没有查询到数据");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error("数据异常");
        }
    }


}
